#https://s.taobao.com/search?q=%E4%B9%A6%E5%8C%85&imgfile=&commend=all&ssid=s5-e&search_type=item&sourceId=tb.index&spm=a21bo.2017.201856-taobao-item.1&ie=utf8&initiative_id=tbindexz_20170306
#https://s.taobao.com/search?q=%E4%B9%A6%E5%8C%85&imgfile=&commend=all&ssid=s5-e&search_type=item&sourceId=tb.index&spm=a21bo.2017.201856-taobao-item.1&ie=utf8&initiative_id=tbindexz_20170306&bcoffset=3&ntoffset=3&p4ppushleft=1%2C48&s=44
import requests,re
def getHTMLText(url):
    try:
        headers = {
            # 请求数据格式
            'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
            # 在浏览器当中的用户信息->用户信息
            'cookie': 'UM_distinctid=1716941d927652-0fb3ea40be025c-335e4e71-144000-1716941d929197; __login_his_sync=0; tourist_expires=1; Hm_lvt_407473d433e871de861cf818aa1405a1=1586608862; _GUID=d8eec002-35da-1ad7-ac12-9ca1c3cb4309; CNZZDATA1261814609=674853875-1586608471-https%253A%252F%252Fwww.mkzhan.com%252F%7C1586608471; readMode=scroll; CNZZDATA1262045698=1069876723-1586605088-https%253A%252F%252Fwww.baidu.com%252F%7C1586607119; redirect_url=%2F211692%2F; Hm_lpvt_407473d433e871de861cf818aa1405a1=1586610847; cn_1262045698_dplus=%7B%22distinct_id%22%3A%20%221716941d927652-0fb3ea40be025c-335e4e71-144000-1716941d929197%22%2C%22%24_sessionid%22%3A%200%2C%22%24_sessionTime%22%3A%201586610857%2C%22%24dp%22%3A%200%2C%22%24_sessionPVTime%22%3A%201586610857%7D',
            # 随机生成浏览器代理
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3756.400 QQBrowser/10.5.4039.400',
            # 防盗链接（请求来源）
            'referer': 'https://www.mkzhan.com/search/?keyword=%E6%96%97%E7%BD%97%E5%A4%A7%E9%99%86'
        }
        r=requests.get(url,timeout=30,headers=headers)
        r.raise_for_status()
        r.encoding=r.apparent_encoding
        return r.text
    except:
        return ""

def pasePage(ilt,html):
    # try:
    plt=re.findall('"view_price":"(.*?)"',html)
    tlt=re.findall('"raw_title":"(.*?)"',html)
    for i in range(len(plt)):
        price=eval(plt[i].split(':')[1])
        title = eval(tlt[i].split(':')[1])
        ilt.append(price,title)
# except:
    #     print('')
def printGoodsList(ilt):
    tplt="{:4}\t{:8}\t{:16}"
    print(tplt.format('序号','价格','商品名称'))
    count=0
    for g in ilt:
        count=count+1
        print(tplt.format(count,g[0],g[1]))
def main():
    goods='书包'
    depth=2
    start_url='https://s.taobao.com/search?q='+goods
    infoList=[]
    for i in range(depth):
        try:
            url=start_url+'&s'+str(44*i)
            html=getHTMLText(url)
            pasePage(infoList,html)
        except:
            continue
    printGoodsList(infoList)

main()